package com.lunhui.service.provider.config;

import com.github.pagehelper.PageHelper;
import net.bull.javamelody.MonitoringFilter;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author renminghe-kg
 */
@Configuration
public class AppConfig {

    @Bean(name = "monitoringFilter")
    public org.springframework.boot.web.servlet.FilterRegistrationBean filterMonitoringFilter() {
        org.springframework.boot.web.servlet.FilterRegistrationBean filterRegistrationBean = new org.springframework.boot.web.servlet.FilterRegistrationBean();
        filterRegistrationBean.setFilter(new MonitoringFilter());
        filterRegistrationBean.setEnabled(true);
        filterRegistrationBean.addUrlPatterns("/*");
        Map<String, String> initParameters = new HashMap<>();
        initParameters.put("contextConfigLocation", "classpath:net/bull/javamelody/monitoring-spring.xml");
        filterRegistrationBean.setInitParameters(initParameters);
        return filterRegistrationBean;
    }

    @Bean
    PageHelper pageHelper(){
        //分页插件
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("returnPageInfo", "check");
        properties.setProperty("params", "count=countSql");
        pageHelper.setProperties(properties);
        //添加插件
        new SqlSessionFactoryBean().setPlugins(new Interceptor[]{pageHelper});
        return pageHelper;
    }
}